/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OTAMYSQLSTORAGEENGINE_H
#define OTAMYSQLSTORAGEENGINE_H

/**
	@author Warren Kenny <warren.kenny@gmail.com>
*/

#ifndef WIN32

#include <mysql/mysql.h>

#else

#include <mysql.h>

#endif

#include <otaCore.h>
#include <otaStorageEngine.h>

#ifndef OTA_MSE_MAX_STRING_SIZE
#define OTA_MSE_MAX_STRING_SIZE 64
#endif

#ifndef OTA_MSE_MAX_QUERY_LENGTH
#define OTA_MSE_MAX_QUERY_LENGTH 5000
#endif

class otaMySQLStorageEngine : public otaStorageEngine
{
public:
	otaMySQLStorageEngine();
	
	virtual bool init( otaConfigReader &config );
	
	virtual bool reset();
	
	virtual void storeSessionInformation( otaSession &session );

	virtual bool close();
	
	bool createPreparedStatements();
	
	~otaMySQLStorageEngine();

	
protected:
	
	bool createTables();
	
	std::string m_serverIP;
	unsigned int m_serverPort;

	std::string m_username;
	std::string m_password;

	std::string m_database;
	std::string m_tablePrefix;
	
	bool m_connected;
	
	MYSQL mysql;
	
	MYSQL_STMT* m_sessionDataQuery;
	
	MYSQL_STMT* m_clientCommandQuery;
	
	MYSQL_STMT* m_clientQueryText;
	
	MYSQL_BIND m_sessionDataBind[6];
	
	MYSQL_BIND m_queryDataBind[13];
	
	MYSQL_BIND m_queryTextBind[2];
	
	unsigned long 	m_bindServerAddress;
	unsigned short 	m_bindServerPort;
	unsigned long 	m_bindClientAddress;
	unsigned short 	m_bindClientPort;
	 
	char		m_bindUsername[OTA_MSE_MAX_STRING_SIZE];
	unsigned long	m_usernameLength;
	
	unsigned int	m_bindThreadID;
	
	unsigned long 	m_sessionID;
	unsigned long 	m_sequence;
	
	otaTime		m_queryBegin;
	otaTime		m_queryEnd;
	
	unsigned int	m_exitCode;
	
	unsigned int	m_rowsReturned;
	
	unsigned int 	m_bytesReceived;
	unsigned int	m_bytesSent;
	
	unsigned int	m_packetsReceived;
	unsigned int	m_packetsSent;
	
	unsigned char	m_queryType;
	
	char		m_bindDatabase[OTA_MSE_MAX_STRING_SIZE];
	unsigned long	m_databaseLength;
	
	unsigned int	m_rule;  
		
	char		m_bindQueryText[OTA_MSE_MAX_QUERY_LENGTH];
	unsigned long	m_queryTextLength;
};

#endif
